package com.example.socket.server;

import com.example.socket.model.DataPackage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author xiaocg
 */
@Slf4j
public class MyMessageDecode extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        byte[] tag = new byte[4];
        tag[0] = byteBuf.readByte();
        tag[1] = byteBuf.readByte();
        tag[2] = byteBuf.readByte();
        tag[3] = byteBuf.readByte();
        int type = byteBuf.readInt();
        log.info("package type:{}",type);
        int length = byteBuf.readInt();
        log.info("package length:{}",length);
        byte[] value = new byte[length];
        byteBuf.readBytes(value);

        DataPackage dataPackage = new DataPackage(tag,type,length,value);
        list.add(dataPackage);
    }
}
